angular.module('starter')
.controller("selectOfficeCtrl", function ($scope, $stateParams, appBase, httpService) {

    var hopId = $stateParams.hopId || "";
    var hopName = $stateParams.hopName || "";
    var hopaddr = $stateParams.addr || "";
    appBase.hospitalCode = hopId;
    appBase.hospitalName = hopName;
    appBase.addr = hopaddr;
    //console.log(hopId + "he" + hopName);
    $scope.titleName = hopName;

    $scope.offices = [];
    $scope.searchOffices = []; //搜索后页面遍历显示的数组 
    $scope.searchCont = {};
    $scope.clearBtn = function () {
        $scope.searchCont.key = "";
        $scope.searchOffices = [];
    }

    $scope.inputSearch = function () {//input的change事件
        var searchValue = $scope.searchCont.key;
        if (searchValue.length < 2) {
            return;
        }
        $scope.searchOffices = [];
 
        angular.forEach($scope.offices, function (modal) {
            angular.forEach(modal.office_data, function (smodal) {
                if (smodal.name.indexOf(searchValue) >= 0)
                {
                    $scope.searchOffices.push(
                        {
                            "pName": modal.name,
                            "sName": smodal.name,
                            "scode": smodal.code
                        });
                }
            });
        });
    };
	
	$scope.liIndex = 0;
	$scope.navClick = function(idx){//点击切换科室
		$scope.liIndex = idx;
		
		if($scope.liIndex!==0){//清除不是历史记录列的滑动效果
			$(".office_box").eq($scope.liIndex).find(".item-content").removeClass("item-content");
		}
		
	}
	
	$scope.onItemDelete = function (item) {//每条历史记录删除
	    $scope.offices[0].office_data.splice($scope.offices[0].office_data.findIndex(x=>x.code == item), 1);
	    localStorage.setItem("hospitalOffices" + appBase.hospitalCode, angular.toJson($scope.offices[0].office_data));
	}
	
	$scope.distoryFn = function(){//清除所有历史记录
	    $scope.offices[0].office_data = [];
	    localStorage.removeItem("hospitalOffices" + appBase.hospitalCode);
	}

    // 医院科室数据
	function loadDepartment() {
	    $scope.offices = [
		{
		    "name":"历史挂号记录",
		    "office_data": []
		}];
	    angular.forEach(angular.fromJson(localStorage.getItem("hospitalOffices"+appBase.hospitalCode)) || [], function (modal) {
	        $scope.offices[0].office_data.push(
                {
                    "name": modal.name,
                    "code":modal.code
                });
	    });

	    var parData = "?hospitalCode=" + appBase.hospitalCode;
	    httpService.get(httpService.appWebApi.getDepartment + parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              angular.forEach(data.returnData, function (modal) {
                  $scope.offices.push(modal);
              });
          }
          , function (error) {
              console.log(error);
          }).finally(function () {
            
          });
	}

	$scope.$watch('$viewContentLoaded', function () {
	    // 你的代码
	    loadDepartment();
	});
	
})
